package barcode.code.nhat.iuhbarcode;

import android.app.ActionBar;
import android.app.Activity;

import android.app.AlertDialog;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.widget.DrawerLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import Database.SQLquery;
import Fragment.Create_Fragment;
import Fragment.Excel_Fragment;
import Fragment.Home_fragment;
import Fragment.N_Fragment;
import Fragment.Process_Fragment;
import Fragment.Table_Fragment;
import Fragment.Zbar_fragment;
import Fragment.Zxing_fragment;
import Library.IntentIntegrator;
import Library.IntentResult;
import Library.NavigationDrawerFragment;

import static android.widget.Toast.makeText;


public class Main_Action extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    private NavigationDrawerFragment mNavigationDrawerFragment;
    private CharSequence mTitle;
    private String class_name = "1100111388";
    // slide menu items
    private String[] navMenuTitles;
    private MediaPlayer mp;
    private SQLquery login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        login = new SQLquery(this);
        login = login.open();
        login.getDatabaseInstance();

        mp = MediaPlayer.create(this,R.raw.exit);
        mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
        // Set up the drawer.
        mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
    }

    @Override
    public void onNavigationDrawerItemSelected(int position) {
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        switch (position) {
            case 0:
                fragmentManager.beginTransaction().replace(R.id.container, Home_fragment.newInstance(position + 1, class_name)).commit();
                break;
            case 1:
                fragmentManager.beginTransaction().replace(R.id.container, Zbar_fragment.newInstance(position + 1, class_name)).commit();
                break;
            case 2:
                fragmentManager.beginTransaction().replace(R.id.container, Zxing_fragment.newInstance(position + 1)).commit();
                break;
            case 3:
                fragmentManager.beginTransaction().replace(R.id.container, Table_Fragment.newInstance(position + 1)).commit();
                break;
            case 4:
                fragmentManager.beginTransaction().replace(R.id.container, Create_Fragment.newInstance(position + 1)).commit();
                break;
            case 5:
                fragmentManager.beginTransaction().replace(R.id.container, Excel_Fragment.newInstance(position + 1)).commit();
                break;
            case 6:
                fragmentManager.beginTransaction().replace(R.id.container, Process_Fragment.newInstance(position + 1)).commit();
                break;
            default:
                fragmentManager.beginTransaction().replace(R.id.container, N_Fragment.newInstance(position + 1)).commit();
                break;

        }


    }

    public void onSectionAttached(int number) {
        setTitle(navMenuTitles[number-1]);
    }

    public void restoreActionBar() {
        ActionBar actionBar = getActionBar();
        assert actionBar != null;
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle(mTitle);
    }

    @Override
    public void setTitle(CharSequence title) {
        ActionBar actionBar = getActionBar();
        mTitle = title;
        assert actionBar != null;
        actionBar.setTitle(mTitle);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (!mNavigationDrawerFragment.isDrawerOpen()) {
            getMenuInflater().inflate(R.menu.main__action, menu);
            restoreActionBar();
            return true;
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_about) {
            Toast.makeText(getApplication(),"about",Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        mp.start();
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            AlertDialog alertDialog = new AlertDialog.Builder(this).create();
            alertDialog.setTitle("Exit Alert");
            alertDialog.setIcon(R.drawable.logout);

            alertDialog.setMessage("Do you really want to exit?");
            alertDialog.setButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //finish();
                    System.exit(0);
                }
            });
            alertDialog.setButton2("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            alertDialog.show();

            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private GregorianCalendar cal = (GregorianCalendar) GregorianCalendar.getInstance();
    private SimpleDateFormat date_format = new SimpleDateFormat("dd-MM-yyyy");
    private TextView t1, t2;
    Zxing_fragment zx = new Zxing_fragment();

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
        t1 = (TextView) findViewById(R.id.txt_code);
        t2 = (TextView) findViewById(R.id.txt_date);

        //ket qua ma vach
        t1.setText(scanResult.getContents());
        //hien thi ngay quet
        t2.setText(date_format.format(cal.getTime()));
        login.insert_student_check(String.valueOf(scanResult.getContents()), class_name, date_format.format(cal.getTime()), zx.getNum());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        login.close();
    }

}
